package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.bo.GoodsCommentBO;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author qugr
 * @create 2020-06-30 18:31
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("submit")
    public BaseRespVO submitOrder(@RequestBody Map<String,Object> map) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        int userId = principal.getId();
        return wxOrderService.submitOrder(map,userId);
    }

    @RequestMapping("prepay")
    public BaseRespVO prepayOrder(@RequestBody Map<String,Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.prepayOrder(map.get("orderId"), (short) 201);
    }

    @RequestMapping("list")
    public BaseRespVO queryOrderList(Integer showType, Integer page, Integer size) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        int userId = principal.getId();
        return wxOrderService.queryOrders(showType,page,size,userId);
    }

    @RequestMapping("detail")
    public BaseRespVO queryOrderInfo(Integer orderId) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.queryOrderInfo(orderId);
    }

    @RequestMapping("cancel")
    public BaseRespVO cancelOrder(@RequestBody Map<String,Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.cancelOrder(map.get("orderId"), (short) 102);
    }

    @RequestMapping("refund")
    public BaseRespVO refund(@RequestBody Map<String,Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.refund(map.get("orderId"), (short) 202);
    }

    @RequestMapping("confirm")
    public BaseRespVO confirm(@RequestBody Map<String,Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.confirm(map.get("orderId"), (short) 401);
    }

    @RequestMapping("goods")
    public BaseRespVO queryOrderGoodsInfo(Integer orderId, Integer goodsId) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.queryOrderGoodsInfo(orderId,goodsId);
    }

    @RequestMapping("delete")
    public BaseRespVO deleteOrder(@RequestBody Map<String,Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        return wxOrderService.deleteOrder(map.get("orderId"));
    }

    @RequestMapping("comment")
    public BaseRespVO createGoodsComment(@RequestBody GoodsCommentBO goodsCommentBO) {Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        if (principal == null) {
            return BaseRespVO.error(404,"登陆超时，请重新登陆");
        }
        int userId = principal.getId();
        goodsCommentBO.setUserId(userId);
        return wxOrderService.createGoodsComment(goodsCommentBO);
    }
}
